<html>
<head><meta charset="utf-8"><title>Poor type inference performance on chained method calls · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html">Poor type inference performance on chained method calls</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212456915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212456915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212456915">(Oct 06 2020 at 17:07)</a>:</h4>
<p>I noticed that type inference for r-a's <code>on_request</code> method shows up in profiles a lot:</p>
<div class="codehilite"><pre><span></span><code>                 9870ms - infer:wait @ on_request
                     9870ms - infer_query
                            0ms - body_with_source_map_query (1 calls)
                            0ms - crate_def_map:wait (560 calls)
                            0ms - generic_params_query (43 calls)
                         9824ms - trait_solve_query (337 calls)
</code></pre></div>


<p>It contains a lot of chained method calls, and rustc has historically had problems with that. Maybe chalk does too?</p>



<a name="212457407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212457407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212457407">(Oct 06 2020 at 17:11)</a>:</h4>
<p>Not only rustc: kotlinc OOMs on IntelliJ Rust here: <a href="https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/ide/formatter/impl/spacing.kt#L35-L36">https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/ide/formatter/impl/spacing.kt#L35-L36</a></p>



<a name="212478359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212478359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212478359">(Oct 06 2020 at 20:07)</a>:</h4>
<p>Interesting! Would love to have a repro so we can use it as a chalk benchmark, but that might be hard with the amount of types involved</p>



<a name="212478853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212478853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212478853">(Oct 06 2020 at 20:10)</a>:</h4>
<p>Simply chaining some method calls does not do the trick, but maybe there's some blowup somewhere with all the type parameters and callbacks</p>



<a name="212480202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212480202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212480202">(Oct 06 2020 at 20:19)</a>:</h4>
<p>Oh, every single method call in the chain ends with a <code>?</code>, that might explain it</p>



<a name="212480380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212480380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212480380">(Oct 06 2020 at 20:20)</a>:</h4>
<p>There aren't really many type params involved, and no deeply nested types should be created by this, but I can see why all the <code>?</code> chaining might be bad</p>



<a name="212481374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212481374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212481374">(Oct 06 2020 at 20:28)</a>:</h4>
<p>Hmm, so here's a more detailed profile:</p>
<div class="codehilite"><pre><span></span><code>                 7328ms - infer:wait @ on_request
                     7328ms - infer_query
                          198ms - trait_solve_query @ Debug
                                0ms - crate_def_map:wait (64 calls)
                                0ms - generic_params_query (4 calls)
                                0ms - impl_datum (1 calls)
                                0ms - item_tree_query (16 calls)
                                1ms - parse_macro_query (17 calls)
                              196ms - ???
                          181ms - trait_solve_query @ Debug
                          184ms - trait_solve_query @ Debug
                          178ms - trait_solve_query @ Debug
                          183ms - trait_solve_query @ Debug
                          179ms - trait_solve_query @ Debug
                          181ms - trait_solve_query @ Debug
                          179ms - trait_solve_query @ Debug
                          180ms - trait_solve_query @ Debug
                          179ms - trait_solve_query @ Debug
                          181ms - trait_solve_query @ Debug
                          179ms - trait_solve_query @ Debug
                          181ms - trait_solve_query @ Debug
                          182ms - trait_solve_query @ Debug
                          182ms - trait_solve_query @ Debug
                          179ms - trait_solve_query @ Debug
                          183ms - trait_solve_query @ Debug
                          180ms - trait_solve_query @ Debug
                          185ms - trait_solve_query @ Debug
                          179ms - trait_solve_query @ Debug
                          181ms - trait_solve_query @ Debug
                          178ms - trait_solve_query @ Debug
                          180ms - trait_solve_query @ Debug
                          181ms - trait_solve_query @ Debug
                          180ms - trait_solve_query @ Debug
                          182ms - trait_solve_query @ Debug
                          182ms - trait_solve_query @ Debug
                          179ms - trait_solve_query @ Debug
                          181ms - trait_solve_query @ Debug
                          181ms - trait_solve_query @ Debug
                          184ms - trait_solve_query @ Debug
                          181ms - trait_solve_query @ Debug
                          183ms - trait_solve_query @ Debug
                          181ms - trait_solve_query @ Debug
                          184ms - trait_solve_query @ Debug
                          179ms - trait_solve_query @ Debug
                          183ms - trait_solve_query @ Debug
                          180ms - trait_solve_query @ Debug
                          180ms - trait_solve_query @ Debug
                            0ms - body_with_source_map_query (1 calls)
                            0ms - crate_def_map:wait (542 calls)
                            0ms - generic_params_query (39 calls)
                          225ms - trait_solve_query (293 calls)
</code></pre></div>


<p>I guess all the time is spent proving that the request parameters implement <code>Debug</code>?</p>



<a name="212481586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212481586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212481586">(Oct 06 2020 at 20:30)</a>:</h4>
<p>Probably the Debug bound here:</p>
<div class="codehilite"><pre><span></span><code>    pub(crate) fn on&lt;R&gt;(
        &amp;mut self,
        f: fn(GlobalStateSnapshot, R::Params) -&gt; Result&lt;R::Result&gt;,
    ) -&gt; Result&lt;&amp;mut Self&gt;
    where
        R: lsp_types::request::Request + &#39;static,
        R::Params: DeserializeOwned + Send + fmt::Debug + &#39;static,
        R::Result: Serialize + &#39;static,
    {
</code></pre></div>



<a name="212481723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212481723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212481723">(Oct 06 2020 at 20:31)</a>:</h4>
<p>yeah</p>



<a name="212481918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212481918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212481918">(Oct 06 2020 at 20:33)</a>:</h4>
<p>I feel like this shouldn't take that long? All the <code>Debug</code>s are derived on non-generic types, so I'd expect almost all of them to be proven immediately (unless I grossly misunderstand how the trait system works)</p>



<a name="212482556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212482556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212482556">(Oct 06 2020 at 20:38)</a>:</h4>
<p>There's some log output like <code>impls_for_trait returned 4822 impls</code>, but it's hard to judge the amount of work being done with such huge output</p>



<a name="212482908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212482908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212482908">(Oct 06 2020 at 20:41)</a>:</h4>
<p>More verbose logs look like this:</p>
<div class="codehilite"><pre><span></span><code>  185ms - trait_solve_query @ &lt;&lt;SelectionRangeRequest as Request&gt;::Params as Debug&gt;
  180ms - trait_solve_query @ &lt;&lt;MatchingBrace as Request&gt;::Params as Debug&gt;
  182ms - trait_solve_query @ &lt;&lt;MemoryUsage as Request&gt;::Params as Debug&gt;
  182ms - trait_solve_query @ &lt;&lt;AnalyzerStatus as Request&gt;::Params as Debug&gt;
  177ms - trait_solve_query @ &lt;&lt;SyntaxTree as Request&gt;::Params as Debug&gt;
  188ms - trait_solve_query @ &lt;&lt;ExpandMacro as Request&gt;::Params as Debug&gt;
</code></pre></div>


<p>It seems like it might just be doing a bunch of work for <em>every single</em> <code>Debug</code> impl?</p>



<a name="212484007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212484007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212484007">(Oct 06 2020 at 20:51)</a>:</h4>
<p>Yeah, it might be searching all types in scope to check for possible answers there</p>



<a name="212484050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212484050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212484050">(Oct 06 2020 at 20:51)</a>:</h4>
<p>starting to look more like a bug than a perf problem</p>



<a name="212523078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Poor%20type%20inference%20performance%20on%20chained%20method%20calls/near/212523078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Poor.20type.20inference.20performance.20on.20chained.20method.20calls.html#212523078">(Oct 07 2020 at 07:31)</a>:</h4>
<p>it's probably the same problem that slows Diesel down, which has been waiting on <a href="https://github.com/rust-lang/chalk/pull/401">https://github.com/rust-lang/chalk/pull/401</a> to be fixed</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>